package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RequestInfo extends ServletMaestro {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// Para crear un aexcepcion arrayIndexOfBoundEception
		int[] lista = new int[3];

		int num = lista[5];
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		Enumeration e = request.getHeaderNames();

		HttpSession session = request.getSession(true);
		Enumeration parametrosSession = session.getAttributeNames();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Pagina con los aprametros recibidos por Request con el metodo get</title>");
		out.println("</head>");
		out.println("<body>");

		if (parametrosSession.hasMoreElements()) {
			out.println("<h3>Parametros de session</h3>");
		}
		while (parametrosSession.hasMoreElements()) {
			String name = (String) parametrosSession.nextElement();
			String value = session.getAttribute(name).toString();
			out.println(name + " = " + value + "<br/>");

		}
		out.println("<h3>Ejemplo de parametros recibidos por request con el metodo get</h3>");
		out.println("Nombre recibido: " + request.getParameter("nombre")
				+ "<br/>");
		session.setAttribute("nombre", request.getParameter("nombre"));
		out.println("Apellido recibido: " + request.getParameter("apellido")
				+ "<br/>");

		out.println("<hr/>");
		out.println("<h2>Parametros de cabecera: </h2><br/>");
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = request.getHeader(name);
			out.println(name + " = " + value + "<br/>");
		}
		out.println("Informacion del servidor: "
				+ session.getServletContext().getServerInfo() + "<br/>");
		out.println("<a href=\"index.jsp\" >Volver </a><br/>");

		out.println("</body>");
		out.println("</html>");
	}

	/**
	 * We are going to perform the same operations for POST requests as for GET
	 * methods, so this method just sends the request to the doGet method.
	 */

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}
}
